package com.drug.mingkang.controller;
import com.drug.mingkang.mapper.HqRawmaterialTypeMapper;
import com.drug.mingkang.model.HqRawmaterialType;
import com.drug.mingkang.model.Material;
import com.drug.mingkang.service.HqRawmaterialService;
import com.drug.mingkang.utils.ResponseMessage;
import com.drug.mingkang.utils.Shit;
import com.drug.mingkang.utils.Tools;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.io.IOException;
import java.util.*;

/**
 * (Material)表控制层
 * @author ll
 * @since 2020-08-06 20:09:09
 */
@RestController
@RequestMapping("material")
public class HqRawmaterialController {
    /**
     * 服务对象
     */
    @Resource
    private HqRawmaterialService hqRawmaterialService;

    @Resource
    private HqRawmaterialTypeMapper mapper;


    ResponseMessage responseMessage = new ResponseMessage();

    @Resource
    private Tools tools;
    /**
     * 通过主键查询单条数据
     *
     * @param materialId
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public Material selectOne(String  materialId) {
        return this.hqRawmaterialService.queryById(materialId);
    }

     /**
     * 显示列表
     * @param material
     * @return
     */
    @RequestMapping("selectAll")
    public Map<String,Object> selectAll(Material material){
        List<Material> list = this.hqRawmaterialService.queryAllByLimit(material);
        int count = this.hqRawmaterialService.selectCount(material);
        return tools.limit(list,count);
    }

    /**
     * 默认加载
     */
    public static String url = "http://www.qyzyc.com/zxzx.html";
    @RequestMapping("add")
    public void add() {
        try {
            Document doc = Jsoup.connect(url).get();
            Elements ele = doc.select(".arccontent ul li");
//            List<String> sd= mapper.selectId();
            for (Element e : ele) {
                String fname = e.text();
                Elements a = e.parent().nextElementSiblings();
                if (a.size() >= 15) {
                    String id=UUID.randomUUID().toString();
                    HqRawmaterialType type = new HqRawmaterialType();
                    type.setTypeId(id);
                    type.setTypeName(fname);
                    mapper.insert(type);
                    for (int i = 1; i < 15; i++) {
                        Element q = a.get(i);
                        String name = q.text();
                        if (!name.equals("")) {
                            Material material = new Material();
                            Double s = Shit.GetDouble();
                            material.setMaterialId(UUID.randomUUID().toString());
                            material.setMaterialName(name);
                            material.setTypeId(id);
                            material.setMoney(s);
                            hqRawmaterialService.insert(material);
                        }
                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    /**
     * 删除方法
     */
    @RequestMapping("deleteById")
    public ResponseMessage delete(String id){
        List<Material>  materials = hqRawmaterialService.selectDlt(id);
        if(materials!=null){
            responseMessage.setStatus(0);
            responseMessage.setMessage("此药材正在使用不能删除");
        }else if(hqRawmaterialService.deleteById(id)>0){
            responseMessage.setStatus(1);
            responseMessage.setMessage("删除成功");
        }else {
            responseMessage.setStatus(0);
            responseMessage.setMessage("删除失败");
        }
        return responseMessage;
    }

    @RequestMapping("insertOn")
    @ResponseBody
    public ResponseMessage inserts(Material material){
        int rows = 0;

        if (material.getMaterialId()!=null  && ! material.getMaterialId().equals("")){
            rows = hqRawmaterialService.update(material);
        }else{
                material.setMaterialId(UUID.randomUUID().toString());
            rows = hqRawmaterialService.insert(material);
        }
        if (rows>0){
            responseMessage.setMessage("操作成功");
            responseMessage.setStatus(1);
        }else {
            responseMessage.setMessage("操作失败");
            responseMessage.setStatus(0);
        }
        System.out.println(rows);
        return responseMessage;
    }

    public static void main(String[] args) {
        Double s = Shit.GetDouble();
    }

}